//后台文章编辑模块
layui.define(['jquery', 'layer', 'upload', 'laytpl'], function(exports) {
    var content = {};
    var $ = layui.jquery,
        layer = layui.layer,
        laytpl = layui.laytpl,
		upload = layui.upload; 
    var ii;

    var imgTpl = $('#imglist-tpl').html();
    var fileTpl = $('#filelist-tpl').html();


    var contentId = $('#id').val();
    var group = $('#group').val();
    if (contentId != "" && group != "") {
        //如果文章已经存在，则直接加载文章下面的图片与附件信息
        var condition = 'fk_id|_|' + contentId + '__fk_group|_|' + group;
        load_images(condition);
        load_files(condition);
    }

    function load_images(condition) {
        $.get('/content/image/getdata?condition=' + condition, function(ret) {
            if (ret.status) {
            	
                var imghtml = "";
                for (var i = 0; i < ret.data.data.length; i++) {
                    laytpl(imgTpl).render(ret.data.data[i], function(html) {
                        imghtml += html;
                    });
                }
                
                $('#imglist').html(imghtml);
                listen_del_img(); //监听删除
                listen_img_click(); //监听点击
            }
        });
    }

    function load_files(condition) {
        $.get('/content/file/getdata?condition=' + condition, function(ret) {
            if (ret.status) {
                var filehtml = "";
                for (var i = 0; i < ret.data.data.length; i++) {
                    laytpl(fileTpl).render(ret.data.data[i], function(html) {
                        filehtml += html;
                    });
                }
                $('#filelist').html(filehtml);
                listen_del_file(); //监听删除

            }
        });
    }

    upload.render({
        url: '/content/image/qiniu_upload?type=image',
        elem: '#fengmianupload',
        accept: 'jpg|png|gif|bmp|jpeg',
        method: 'post',
        before: function() {
            ii = layer.msg('图片上传中...', {
                icon: 16,
                time: 10000
            });
        },
        done: function(ret) {
            if (ret.status) {
            	console.log(ret);
                change_fengmian(ret.data.url);
                layer.close(ii);
            } else {
                layer.alert(ret.info);
            }
        }
    });


    if ($('#fengmian').val() != "") {
        change_fengmian($('#fengmian').val());
    }

    //监听封面图改变
    $('#fengmian').change(function() {
        change_fengmian($('#fengmian').val());
    });

    function change_fengmian(url) {
        $('#fengmian').val(url);
        $('#fengmian-box').html('<img src="' + url + '">');
        $('#fengmian-box').unbind('click').click(function() {
            var $this = $(this);
            //调用示例
            layer.photos({
                photos: '#fengmian-box',
                anim: 5 //0-6的选择，指定弹出图片动画类型，默认随机（请注意，3.0之前的版本用shift参数）
            });
        });
    }

    upload.render({
        url: '/content/image/qiniu_upload?type=image',
        elem: '#imageupload',
        accept: 'jpg|png|gif|bmp|jpeg',
        method: 'post',
        before: function() {
            ii = layer.msg('图片上传中...', {
                icon: 16,
                time: 10000
            });
        },
        done: function(ret) {
        	
            if (ret.status) {
            	console.log(ret);
                laytpl(imgTpl).render(ret.data, function(html) {
                    $('#imglist').append(html);
                    listen_del_img(); //监听删除
                    listen_img_click(); //监听点击
                });
                layer.close(ii);
            } else {
                layer.alert(ret.info);
            }
        }
    });
    //监听图片上传
    function listen_del_img() {
        //需要先解除原先绑定的事件，防止重复执行
        $('.del-image').unbind('click').click(function() {
            var id = $(this).attr('data-id');
            var ii = layer.confirm("确定删除这张图片吗？", function() {
                layer.close(ii);
                $.get('/content/image/del/ids/' + id, function(ret) {
                    if (ret.status) {
                        $('#img-box-' + id).remove();
                    } else {
                        layer.alert(ret.info);
                    }
                });
            });
        })
    }

    function listen_img_click() {
        $('.img-wrap').unbind('click').click(function() {
            var $this = $(this);
            //调用示例
            layer.photos({
                photos: '#imglist',
                anim: 5 //0-6的选择，指定弹出图片动画类型，默认随机（请注意，3.0之前的版本用shift参数）
            });
        });
    }

    upload.render({
        url: '/content/file/qiniu_upload?type=file',
        elem: '#fileupload',
        accept: 'pdf|doc|docx|xls|xlsx|ppt|htm|html|txt|zip|rar|gz|bz2',
        method: 'post',
        before: function() {
            ii = layer.msg('文件上传中...', {
                icon: 16,
                time: 10000
            });
        },
        done: function(ret) {
            if (ret.status) {
                laytpl(fileTpl).render(ret.data, function(html) {
                    $('#filelist').append(html);
                    listen_del_file(); //监听删除
                });
                layer.close(ii);
            } else {
                layer.alert(ret.info);
            }
        }
    });

    //监听删除文件
    function listen_del_file() {
        //需要先解除原先绑定的事件，防止重复执行
        $('.del-file').unbind('click').click(function() {
            var id = $(this).attr('data-id');
            var ii = layer.confirm("确定删除这个文件吗？", function() {
                layer.close(ii);
                $.get('/content/file/del/ids/' + id, function(ret) {
                    if (ret.status) {
                        $('#file-box-' + id).remove();
                    } else {
                        layer.alert(ret.info);
                    }
                });
            });
        })
    }
    exports('content', content); //对外接口
});